package cn.jbolt.common.poi.excel;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;

import cn.hutool.core.collection.CollUtil;
/**
 * JBolt提供Excel导出封装类
 * @ClassName:  JBoltExcel   
 * @author: JFinal学院-小木 QQ：909854136 
 * @date:   2020年3月10日   
 */
public class JBoltExcel {
	private String fileName;//导出文件名
	private boolean isBig;//格式设置
	private boolean isXlsx;//格式设置
	private List<JBoltExcelMerge> merges;//合并设置
	private List<JBoltExcelHeader> headers;//表头
	private List<JBoltExcelData> datas;//数据
	private boolean mergeAsHeader;//用merge代替了header内容设置
	private JBoltExcelDataChangeHandler dataChangeHandler;
	public JBoltExcel() {
		this.isBig=false;
		this.isXlsx=false;
		this.mergeAsHeader=false;
	}
	public boolean hasDatas() {
		return CollUtil.isNotEmpty(this.datas);
	}
	public boolean hasHeaders() {
		return CollUtil.isNotEmpty(this.headers);
	}
	public boolean hasMerges() {
		return CollUtil.isNotEmpty(this.merges);
	}
	public static JBoltExcel create() {
		return new JBoltExcel();
	}
	public boolean isXlsx() {
		return isXlsx;
	}
	public JBoltExcel setXlsx(boolean isXlsx) {
		if(this.isBig) {
			this.isXlsx=true;
		}else {
			this.isXlsx = isXlsx;
		}
		return this;
	}
	public List<JBoltExcelMerge> getMerges() {
		return merges;
	}
	public JBoltExcel setMerges(List<JBoltExcelMerge> merges) {
		this.merges = merges;
		return this;
	}
	 
	public List<JBoltExcelData> getDatas() {
		return datas;
	}
	public JBoltExcel setDatas(List<JBoltExcelData> datas) {
		this.datas = datas;
		return this;
	}
	public JBoltExcel setModelDatas(List<? extends Model<?>> models) {
		if(models!=null&&models.size()>0) {
			this.datas=new ArrayList<JBoltExcelData>();
			for(Model<?> model:models) {
				this.datas.add(JBoltExcelData.fromModel(model));
			}
		}
		return this;
	}
	/**
	 * change数据
	 * @param data
	 * @param dataChangeHandler
	 * @return
	 */
	public JBoltExcel setDataChangeHandler(JBoltExcelDataChangeHandler dataChangeHandler) {
		this.dataChangeHandler=dataChangeHandler;
		return this;
	}
	public JBoltExcel setRecordDatas(List<Record> records) {
		if(records!=null&&records.size()>0) {
			this.datas=new ArrayList<JBoltExcelData>();
			for(Record record:records) {
				this.datas.add(JBoltExcelData.fromRecord(record));
			}
		}
		return this;
	}
 
	public List<JBoltExcelHeader> getHeaders() {
		return headers;
	}
	public JBoltExcel setHeaders(List<JBoltExcelHeader> headers) {
		this.headers = headers;
		return this;
	}
	public JBoltExcel setHeaders(JBoltExcelHeader... headers) {
		this.headers = CollUtil.newArrayList(headers);
		return this;
	}
	public boolean isMergeAsHeader() {
		return mergeAsHeader;
	}
	public JBoltExcel setMergeAsHeader(boolean mergeAsHeader) {
		this.mergeAsHeader = mergeAsHeader;
		return this;
	}
	
	public byte[] toByteArray() {
		return JBoltExcelUtil.getExcelBytes(this);
	}
	public boolean isBig() {
		return isBig;
	}
	public JBoltExcel setBig(boolean isBig) {
		this.isBig = isBig;
		if(isBig) {
			this.isXlsx=true;
		}
		return this;
	}
	public JBoltExcelDataChangeHandler getDataChangeHandler() {
		return dataChangeHandler;
	}
	public String getFileName() {
		return fileName;
	}
	public JBoltExcel setFileName(String fileName) {
		this.fileName = fileName;
		return this;
	}
}
